from pymongo.results import UpdateResult
from lina.mongodb import db
from bson import ObjectId

from lina.service.BaseService import BaseService




class CaseService(BaseService):

    def __init__(self):
        self.table_name = 'case'
        super().__init__()

    def fetch_cases_by_phase_ids(self, ids):
        # 获取给定多个列表下所有案件
        table = db()[self.table_name]
        phase_ids = []

        for oid in ids:
            newId = ObjectId(oid)
            phase_ids.append(newId)

        case_list = []
        for case_item in table.find({"phase_id": {"$in": phase_ids}}):
            case_list.append(case_item)

        return case_list

    def update_tags_by_case_id(self, case_id, tags):
        # 更新一个案件的所有标签
        table = db()[self.table_name]

        updateResult = table.update_one(
            {"_id": ObjectId(case_id)}, {"$set": {"tags": tags}})
        if updateResult.modified_count == 1:
            return True
        return False

    def fetch_case_by_id(self, case_id):
        # 查询一个案件的详情
        table = db()[self.table_name]
        caseId = ObjectId(case_id)
        return table.find_one({"_id": caseId})

    
    